//引入express模块
const express = require('express');
//引入连接池模块
const pool = require('../pool.js');
//console.log(pool);
//创建路由器对象
const r = express.Router();

//查询文章标签列表
r.get("/sort/:subject_id",(req,res,next)=>{
  let obj=req.params;
  // console.log(obj);
	pool.query("select * from cg_sort where subject_id=?",[obj.subject_id],(err,result)=>{
		if(err){
      next(err);
      return
    };
    if(result.length===0){
      res.send({code:201,msg:"该分类不存在"});
    }else{
      res.send({code:200,msg:"查询成功",result:result});
      // console.log(result);
    }
	});
});


//根据aid查询文章列表
// action="http://127.0.0.1:3000/v1/article/list/1"
r.get('/list/:aid',(req,res,next)=>{
  let obj=req.params;
  console.log(obj);
	pool.query("select * from cg_article where aid=?",[obj.aid],(err,result)=>{
		if(err){
      next(err);
      return
    };
    if(result.length===0){
		  res.send({code:201,msg:'该用户没有文章'});
		}else{
		  res.send({code:200,msg:"查询成功",result:result});
      // console.log(result);
		}
	});
});

//查询文章列表
r.get('/lists',(req,res,next)=>{
	pool.query("select * from cg_article",(err,result)=>{
		if(err){
      next(err);
      return
    };
    if(result.length===0){
		  res.send({code:201,msg:'该用户没有文章'});
		}else{
		  res.send({code:200,msg:"查询成功",result:result});
      console.log(result);
		}
	});
});

//查询文章列表
//根据uid查询文章步骤
// action="http://127.0.0.1:3000/v1/article/list/1"
r.get('/step/:article_id',(req,res,next)=>{
  let obj=req.params;
  console.log(obj);
	pool.query("select * from cg_article_step where article_id=?",[obj.article_id],(err,result)=>{
		if(err){
      next(err);
      return
    };
    if(result.length===0){
		  res.send({code:201,msg:'查询失败'});
		}else{
		  res.send({code:200,msg:"查询成功",result:result});
      // console.log(result);
		}
	});
});

//根据uid查询文章步骤图
// action="http://127.0.0.1:3000/v1/article/list/1"
r.get('/lists/:aid',(req,res,next)=>{
  let obj=req.params;
  
	pool.query("select * from cg_article where aid=?",[obj.aid],(err,result1)=>{
    // pool.query("select * from cg_article left join cg_article_step on aid=article_id",[obj.aid],(err,result)=>{
		if(err){
      next(err);
      return
    };
    console.log(obj);
    pool.query("select step1,step2,step3,step4,step5,step6 from cg_article_step where article_id=?",[obj.aid],(err,result2)=>{
      if(result2.length===0){
        res.send({code:201,msg:'查询失败'});
      }else{
        res.send({code:200,msg:"查询成功",result1:result1,result2:result2});
        // console.log(result);
      }
    });
	});
});
//导出路由器对象
module.exports=r;